package com.qf.ability.search.listener;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * 热搜词统计监听
 *
 * description:
 * author: Ken
 * 公众号：Java架构栈
 */
@Component
public class HotWordListener {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @KafkaListener(groupId = "group-${spring.application.name}", topics = "hot_word_out")
    public void hotHandler(ConsumerRecord record){
        String key = new String((byte[]) record.key());
        Long value = Long.valueOf(new String((byte[])record.value()));

        //操作redis
        stringRedisTemplate.opsForZSet()
                .add("hot_words", key, value);

    }
}
